package com.ruoyi.energy.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.energy.domain.NyCheckReport;
import com.ruoyi.energy.domain.NyCheckSumReport;
import com.ruoyi.energy.domain.vo.MeterTreeVO;
import com.ruoyi.energy.mapper.NyCheckReportMapper;
import com.ruoyi.energy.service.INyCheckMeterService;
import com.ruoyi.energy.service.INyCheckReportService;
import com.ruoyi.omp.api.domain.NyCheckMeter;
import com.ruoyi.omp.api.domain.vo.NyCheckMeterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 抄表报表Service业务层处理
 *
 * @author zmc
 * @date 2022-08-23
 */
@Slf4j
@Service
public class NyCheckReportServiceImpl implements INyCheckReportService {
    @Autowired
    private NyCheckReportMapper nyCheckReportMapper;

    @Autowired
    private INyCheckMeterService nyCheckMeterService;


    /**
     * 查询抄表报表
     *
     * @param id 抄表报表ID
     * @return 抄表报表
     */
/*    @Override
    public NyCheckReport selectNyCheckReportById(String id) {
        return nyCheckReportMapper.selectNyCheckReportById(id);
    }*/

    /**
     * 查询抄表报表列表
     *
     * @param nyCheckReport 抄表报表
     * @return 抄表报表
     */
    @Override
    public List<NyCheckReport> selectNyCheckReportList(NyCheckReport nyCheckReport) {
        //todo if 水表，排除消防；if 排除考核表，列表有顺序，私表，总表
        if(StringUtils.isNotBlank(nyCheckReport.getMeterId())){
            //查询总表抄录树
            NyCheckMeter nyCheckMeter = new NyCheckMeter();
            nyCheckMeter.setMeterId(nyCheckReport.getMeterId());
            nyCheckMeter.setFeeCycle(nyCheckReport.getFeeCycle());
            NyCheckMeterVO nyCheckMeterVO = nyCheckMeterService.selectNyMeterCheckVO(nyCheckMeter);
            //总表抄录树转为List
            List<NyCheckReport> results = new ArrayList<>();
            covertTree2List(nyCheckMeterVO,results);
            if(results.size()>0)
                return results;
        }
        return nyCheckReportMapper.selectNyCheckReportList(nyCheckReport);
    }

    private void covertTree2List(NyCheckMeterVO nyCheckMeterVO, List<NyCheckReport> results) {
        NyCheckReport nyCheckReport = new NyCheckReport();
        copyNyCheckMeterVO(nyCheckMeterVO,nyCheckReport);
        results.add(nyCheckReport);
        if(nyCheckMeterVO.getChildren() != null && nyCheckMeterVO.getChildren().size()>0){
            for(NyCheckMeterVO node:nyCheckMeterVO.getChildren()){
                NyCheckReport tmp = new NyCheckReport();
                copyNyCheckMeterVO(node,tmp);
                results.add(tmp);
            }

        }
        return;
    }

    private void copyNyCheckMeterVO(NyCheckMeterVO nyCheckMeterVO, NyCheckReport nyCheckReport) {
        nyCheckReport.setEnterpriseName(nyCheckMeterVO.getEnterpriseName());
        nyCheckReport.setMeterNum(nyCheckMeterVO.getMeterNum());
        nyCheckReport.setMeterModel(nyCheckMeterVO.getMeterModel());
        nyCheckReport.setInstallPosition(nyCheckMeterVO.getInstallPosition());
        nyCheckReport.setLastValue(nyCheckMeterVO.getLastValue());
        nyCheckReport.setCurrentValue(nyCheckMeterVO.getCurrentValue());
        nyCheckReport.setMagnification(nyCheckMeterVO.getMagnification());
        nyCheckReport.setActualDosage(nyCheckMeterVO.getActualDosage());
        nyCheckReport.setShareDosage(nyCheckMeterVO.getMonthShare());
    }

    @Override
    public List<NyCheckSumReport> selectNyCheckSumReportList(NyCheckReport nyCheckReport) {
        List<NyCheckSumReport> result = new ArrayList<NyCheckSumReport>();
        //水表汇总、电表汇总逻辑不同
        if(nyCheckReport.getMeterType().equals("水表")){
            //总表，考核表,分表汇总量
            //查询考核表
            nyCheckReport.setCategory("考核表");
            List<NyCheckReport> reports = nyCheckReportMapper.selectNyCheckReportList(nyCheckReport);
            //查询一级考核表及私表汇总量
            result = new ArrayList<>(reports.size());
            for(NyCheckReport report:reports){
                //考核表pid,查询总表抄表记录,
                if(StringUtils.isNotBlank(report.getParentId())){
                    nyCheckReport.setCategory("总表");
                    nyCheckReport.setMeterId(report.getParentId());
                    List<NyCheckReport> listZ = nyCheckReportMapper.selectNyCheckReportList(nyCheckReport);
                    if(listZ != null && listZ.size()>0){
                        NyCheckSumReport tmp = new NyCheckSumReport();
                        copyReportToSumKH(report,listZ.get(0),tmp);
                        result.add(tmp);
                    }
                }

            }

        }else if(nyCheckReport.getMeterType().equals("电表")){
            //查询总表记录，计算总表私表汇总量,
            nyCheckReport.setCategory("总表");
            List<NyCheckReport> reports = nyCheckReportMapper.selectNyCheckReportList(nyCheckReport);
            result = new ArrayList<>(reports.size());
            if(reports.size()>0){
                for(NyCheckReport report:reports){
                    NyCheckSumReport tmp = new NyCheckSumReport();
                    copyReportToSum(report,tmp);
                    result.add(tmp);
                }
            }else {
                //如果总表抄表为空，查询总表信息，计算汇总信息。
                //NyCheckSumReport tmp = new NyCheckSumReport();
                log.error("电表汇总没有总表抄表记录");
                //查询总表信息
                //查询总表台账，比较总表抄表记录，进行比对查找总表遗漏，添加builder
                //List<NyMeterAccount2> accountList = this.selectNyAccountByMeter(nyCheckMeter);

            }

        }
        return result;
    }

    private void copyReportToSum(NyCheckReport report, NyCheckSumReport tmp) {
        tmp.setMeterName(report.getInstallPosition());
        tmp.setLastValue(report.getLastValue());
        tmp.setCurrentValue(report.getCurrentValue());
        tmp.setActualDosage(report.getActualDosage());
        NyCheckMeter nyCheckMeter =new NyCheckMeter();
        nyCheckMeter.setFeeCycle(report.getFeeCycle());
        nyCheckMeter.setMeterId(report.getMeterId());
        //总表私表汇总量；
        BigDecimal huizong = nyCheckMeterService.computePrivateSummary(nyCheckMeter);
        tmp.setSumPrivate(huizong);
    }

    private void copyReportToSumKH(NyCheckReport report,NyCheckReport reportZ, NyCheckSumReport tmp) {
        //report 考核表
        tmp.setMeterNameKH(report.getInstallPosition());
        tmp.setLastValueKH(report.getLastValue());
        tmp.setCurrentValueKH(report.getCurrentValue());
        tmp.setActualDosageKH(report.getActualDosage());
        NyCheckMeter nyCheckMeter =new NyCheckMeter();
        nyCheckMeter.setFeeCycle(report.getFeeCycle());
        nyCheckMeter.setMeterId(report.getMeterId());
        //考核表私表汇总量；
        BigDecimal huizong = nyCheckMeterService.computePrivateSummary(nyCheckMeter);
        tmp.setSumPrivate(huizong);

        //设置总表
        tmp.setMeterName(reportZ.getInstallPosition());
        tmp.setLastValue(reportZ.getLastValue());
        tmp.setCurrentValue(reportZ.getCurrentValue());
        tmp.setActualDosage(reportZ.getActualDosage());
        tmp.setMeterId(reportZ.getMeterId());
    }


    /**
     * 新增抄表报表
     *
     * @param nyCheckReport 抄表报表
     * @return 结果
     */
/*    @Override
    public int insertNyCheckReport(NyCheckReport nyCheckReport) {
        return nyCheckReportMapper.insertNyCheckReport(nyCheckReport);
    }*/

    /**
     * 修改抄表报表
     *
     * @param nyCheckReport 抄表报表
     * @return 结果
     */
/*    @Override
    public int updateNyCheckReport(NyCheckReport nyCheckReport) {
        return nyCheckReportMapper.updateNyCheckReport(nyCheckReport);
    }*/

    /**
     * 批量删除抄表报表
     *
     * @param ids 需要删除的抄表报表ID
     * @return 结果
     */
/*    @Override
    public int deleteNyCheckReportByIds(String[] ids) {
        return nyCheckReportMapper.deleteNyCheckReportByIds(ids);
    }*/

    /**
     * 删除抄表报表信息
     *
     * @param id 抄表报表ID
     * @return 结果
     */
/*    @Override
    public int deleteNyCheckReportById(String id) {
        return nyCheckReportMapper.deleteNyCheckReportById(id);
    }*/

    @Override
    public List<MeterTreeVO> selectSumMeterList(NyCheckReport nyCheckReport) {
        return nyCheckReportMapper.selectSumMeterList(nyCheckReport);
    }
}
